package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.FormTempDao;
import com.gccloud.design.dto.FormTempSaveDto;
import com.gccloud.design.entity.FormTempEntity;
import com.gccloud.design.service.FormTempService;
import com.gccloud.design.vo.FormTempVo;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.feign.api.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ma.xiulan
 * @date 2021/05/24/ 10:48
 */
@Service
public class FormTempServiceImpl extends ServiceImpl<FormTempDao, FormTempEntity> implements FormTempService {

    @Resource
    private FormTempDao formTempDao;

    @Resource
    private ISysUserService iSysUserService;

    /**
     * 页面模板页面方法
     */
    @Override
    public Map<String, Object> getFormTemp(Integer businessTypeId, String searchCon, SearchDTO searchDTO) {
        Page<FormTempVo> page = new Page<>(searchDTO.getCurrent(), searchDTO.getSize());
        List<FormTempVo> list = formTempDao.getFormTemp(businessTypeId, searchCon, page);
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", list);
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveFormTemp(FormTempSaveDto formTempSaveDto) {
        FormTempEntity formTempEntity = BeanConvertUtils.convert(formTempSaveDto, FormTempEntity.class);
        formTempEntity.setCreateUserName(iSysUserService.getCurrentUser().getData().getName());
        formTempEntity.setCreateUserId(iSysUserService.getCurrentUser().getData().getId());
        formTempDao.saveFormTemp(formTempEntity);
        int result = formTempDao.updateFormTemp(formTempEntity.getFormId());
        return result > 0;
    }

    @Override
    public Boolean saveUpdateFormTemp(FormTempVo formTempVo) {
        FormTempEntity formTempEntity = BeanConvertUtils.convert(formTempVo, FormTempEntity.class);
        formTempEntity.setUpdateUser(iSysUserService.getCurrentUser().getData().getName());
        int result = formTempDao.saveUpdateFormTemp(formTempEntity);
        return result > 0;
    }

    @Override
    public Boolean deleteFormTempt(Integer formId) {
        int result = formTempDao.deleteFormTempt(formId);
        return result > 0;
    }
}
